package com.module.TraceSource.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import com.module.TraceSource.vo.TsDefEvent;
import com.module.TraceSource.vo.TsDefEventItem;
import com.module.TraceSource.vo.TsEntEvent;
import com.module.TraceSource.vo.TsEntFlow;

import base.dao.GenericHibernateDao;
import base.util.Page;


public class TsEntEventDao extends GenericHibernateDao<TsEntEvent, Long>{
	private static TsEntEventDao dao;
	private TsDefEventItemDao tsDefEventItemDao = TsDefEventItemDao.getInstance();
	private TsEntEventItemDao tsEntEventItemDao = TsEntEventItemDao.getInstance();

	public TsEntEventDao() {
		super();
	}
	
	public synchronized static TsEntEventDao getInstance(){
		if(dao == null){
			dao = new TsEntEventDao();
		}
		return dao;
	}
	
	/**
	 * 获取溯源流程取得溯源流程操作项
	 * @param entFlowIds
	 * @return
	 */
	public List<TsEntEvent> getTsEntEvent(Session session,Map<String,Object> map) {
		String hql = "";
		String in = "";
		
		if(map!=null){
			String entFlowIds = (String) map.get("flowIds");
			String flowId = "";
			
			String id[] = null;

			if (!entFlowIds.equals("")) {
				id = entFlowIds.split("/");
			}
			
			for(int i=0;i<id.length;i++){
				
				flowId = id[i];
				
				if(!flowId.equals("")){
					in = in + "'" + flowId + "',";
				}
			}
		}
		
		in = in.substring(0, in.length()-1);
		
		//按照顺序号排序
		hql = "from TsEntEvent as obj where obj.status !='D' and obj.flowid in (" + in + ") "
				+ " order by obj.flowid,obj.seq ";
		
		Query query = session.createQuery(hql.toString());
		
		List<TsEntEvent> tsEntEvent = query.list();
		
		return tsEntEvent;
	}
	
	/**
	 * 分页列出操作事项列表
	 * @param session
	 * @param obj
	 * @param page
	 * @return
	 */
	public Page getList(Session session, TsEntEvent obj, Page page) {
		String hql1 = "from TsEntEvent as obj where obj.status !='D' ";
		String hql2 = "select count(*) from TsEntEvent as obj where obj.status !='D' ";
		if(obj.getFlowid()!=null && obj.getFlowid()!=0){
			hql1=hql1 +  " and obj.flowid = "+obj.getFlowid();
            hql2=hql2 +  " and obj.flowid = "+obj.getFlowid();
		}
		hql1 +=" order by obj.seq";
		Query query = session.createQuery(hql1);
        query.setFirstResult(page.getStart()).setMaxResults(page.getLimit());
        page.setTotal((long) ((Long) session.createQuery(hql2)
                .uniqueResult()).intValue());
        page.setRows(query.list());
		return page;
	}

	/**
	 * 批量保存TsEntEvent
	 * @param session
	 * @param newTsEntFlow
	 * @param defEventList
	 */
	public void saveEntEventList(Session session, TsEntFlow newTsEntFlow,
			List<TsDefEvent> defEventList) {
		for(TsDefEvent defEvent : defEventList){
			TsEntEvent entEvent = TsEntEvent.copyEntEvent(defEvent);
			entEvent.setEventName(defEvent.getEventName());
			entEvent.setFlowName(newTsEntFlow.getFlowName());
			entEvent.setFlowid(newTsEntFlow.getId());
			entEvent.setOrgid(newTsEntFlow.getOrgid());
			entEvent.setOrgname(newTsEntFlow.getOrgname());
			entEvent.setCreaterid(newTsEntFlow.getCreaterid());
			save(session, entEvent);
			
			List<TsDefEventItem> eventItemList = tsDefEventItemDao.findBySQL(session, "FROM TsDefEventItem obj WHERE obj.status!='D' AND obj.eventid="+defEvent.getId());
			tsEntEventItemDao.saveEventItemList(session,entEvent,eventItemList);
		}
	}
	
	public boolean isExistsByFlowId(Session session,Long flowId){
		SQLQuery query=session.createSQLQuery(" select count(*) from ts_ent_event where i_flowid="+flowId+" and c_status!='D'");
		int n=((java.math.BigInteger)query.uniqueResult()).intValue();
		return n>0?true:false;
	}
	public Integer getEntSeqMaximumValue(Session session,String orgId,Long flowId){
		SQLQuery query=session.createSQLQuery(" select count(*) from ts_ent_event where i_flowid="+flowId+" and c_orgid='"+orgId+"' and c_status!='D'");
		return ((java.math.BigInteger)query.uniqueResult()).intValue();
	}
	public void saveSortSeq(Session session,String orgId,Long flowId,Integer seq,Long id,Integer oldSeq){
		String hql;
		if(oldSeq>seq){
			hql= "from TsEntEvent as obj where obj.status !='D' and orgid='"+orgId+"' and flowid="+flowId+" and id !="+id+" and seq>="+seq+" and seq<"+oldSeq+" order by seq";
			
		}else {
			hql = "from TsEntEvent as obj where obj.status !='D' and orgid='"+orgId+"' and flowid="+flowId+" and id !="+id+" and seq>"+oldSeq+" and seq<="+seq+" order by seq";
		}
		List<TsEntEvent> list = session.createQuery(hql).list();
		for(TsEntEvent t:list){
			if(oldSeq>seq){
			  t.setSeq(t.getSeq()+1);
			}else {
				t.setSeq(t.getSeq()-1);
			}
			this.merge(session, t);
		}
		
	}
	/**
	 * 删除溯源操作事项重新排序
	 * @param session
	 * @param flowId
	 * @param seq
	 * @param id
	 */
	public void deleteSortSeq(Session session,String orgId,Long flowId,Integer seq,Long id){
		String hql = "from TsEntEvent as obj where obj.status !='D' and orgid='"+orgId+"' and flowid="+flowId+" and id !="+id+" and seq>"+seq+" order by seq";
		List<TsEntEvent> list = session.createQuery(hql).list();
		for(TsEntEvent t:list){
			t.setSeq(t.getSeq()-1);
			this.merge(session, t);
		}
	}
	/**
	 * 判断溯源操作事项是否存在
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public boolean isExist(Session session,String eventName,Long flowId,Long id,String orgId ){
		StringBuffer sql = new StringBuffer(" select count(*) from TsEntEvent where eventName='");
		sql.append(eventName).append("' and status !='D' and flowid=").append(flowId);
		sql.append(" and orgid='").append(orgId).append("'");
		if(id != null && id !=0){
			sql.append(" and id !=").append(id);
		}
		Query query=session.createQuery(sql.toString());
		int n=((Long)query.uniqueResult()).intValue();
		return n>0?false:true;
	}
	/**
	 * 根据类型id判断溯源操作事项是否存在
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public boolean isExist2(Session session,Long typeId){
		StringBuffer sql = new StringBuffer(" select count(*) from TsEntEvent where typeId=");
		sql.append(typeId).append(" and status !='D'");
		Query query=session.createQuery(sql.toString());
		int n=((Long)query.uniqueResult()).intValue();
		return n>0?false:true;
	}
	
	
	/**
	 * 根据流程获取事件
	 * @param session
	 * @param flowId
	 * @return
	 * @throws Exception
	 */
	public List<TsEntEvent> findEntEventByFlowId(Session session,String flowId) throws Exception{
		String hql="from TsEntEvent as obj where obj.status !='D' and obj.flowid='"+flowId+"' order by obj.seq";
		List<TsEntEvent> list = dao.findBySQL(session, hql);
		
		return list;
	}
}
